package com.geekbang.work2;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author nhsoft.lul
 */
public class ProcedureApplication {
    static String URL = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
    static String USER = "root";
    static String PASSWORD = "123456";

    /* 存储过程
    delimiter ;;
    drop procedure if exists idata;
    create procedure idata()
    begin
      declare i int;
      set i=1;
      while(i<=100000)do
        insert into book_order(order_num, customer_num, order_total_money, order_discount_money, order_pay_money) values(i, i, i,i,i);
        set i=i+1;
      end while;
    end;;
    delimiter ;
     */

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement statement = null;
        CallableStatement callableStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "delete from book_order";
            statement = conn.prepareStatement(sql);
            statement.execute();

            long start = System.currentTimeMillis();
            conn.setAutoCommit(false);

            callableStatement = conn.prepareCall("{CALL idata}");
            callableStatement.execute();

            conn.commit();
            conn.setAutoCommit(true);
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end - start));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (callableStatement != null) {
                    callableStatement.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
